package com.example.zheng.els2.activity;


import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.zheng.els2.R;

import java.util.Set;

public class Login extends Activity implements View.OnClickListener{

    private Button login;
    private Button register;
    private EditText playName;
    private EditText passWord;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //去掉标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //去掉手机上的状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.login);


        login=(Button)findViewById(R.id.login);
        register=(Button)findViewById(R.id.register);
        playName=(EditText)findViewById(R.id.playName);
        passWord=(EditText)findViewById(R.id.password);


        //绑定监听事件
        login.setOnClickListener(this);
        register.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){

            case R.id.login:
                //得到输入文本的信息
                String name=playName.getText().toString();
                String pass=passWord.getText().toString();
                SharedPreferences data=getSharedPreferences("data1",MODE_PRIVATE);
                Set<String> set=data.getStringSet(name,null);
                if(set==null){
                    Toast.makeText(this,"还没有注册",Toast.LENGTH_SHORT).show();
                }else{
                    for(String s:set){
                        if(s.equals(pass)){
                            Toast.makeText(this,"登录成功",Toast.LENGTH_SHORT).show();
                            Intent intent=new Intent(this,GameActivity.class);
                            intent.putExtra("player",name);
                            startActivity(intent);
                            finish();
                        }else{
                            Toast.makeText(this,"密码不正确",Toast.LENGTH_SHORT).show();
                        }
                    }
                }

                break;

            case R.id.register:
                Intent intent1=new Intent(this,Register.class);
                startActivity(intent1);

                break;
        }

    }
}
